﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Collections;

namespace NotifyDir
{
    public partial class Form1 : Form
    {
        public string m_notifyDir;
        public string m_uploadDir;
        public string m_ip;
        public string m_user;
        public string m_passwd;
        public string m_filter;
        public FileSystemWatcher m_fsWatcher;
        public Hashtable m_modifyFiles;

        public Form1()
        {
            InitializeComponent();
            initCfg("cfg.ini");
            operBox.Text = "[" + DateTime.Now.ToString() + "] " + "stop watch\n";
            m_fsWatcher = new FileSystemWatcher();
            m_modifyFiles = new Hashtable();

            fileBox.Clear();
            fileBox.AppendText("STOP STATUS.");
        }

        public void reload()
        {
            configBox.Clear();
            initCfg("cfg.ini");
            operBox.Text = "[" + DateTime.Now.ToString() + "] " + "stop watch\n";
            m_fsWatcher.EnableRaisingEvents = false;
            m_fsWatcher = new FileSystemWatcher();

            fileBox.Clear();
            fileBox.AppendText("STOP STATUS.");
        }

        public void initCfg(string strCfg)
        {
            StreamReader sr = new StreamReader(strCfg);
            String line;
            if((line = sr.ReadLine()) != null) 
            {
                m_notifyDir = line;
                configBox.AppendText("notify dir : "+ line+"\n");   //增加读出的内容到listbox            
            }
            if ((line = sr.ReadLine()) != null)
            {
                m_uploadDir = line;
                configBox.AppendText("upload dir : " + line + "\n");   //增加读出的内容到listbox            
            }
            if ((line = sr.ReadLine()) != null)
            {
                m_ip = line;
                configBox.AppendText("upload ip : " + line + "\n");   //增加读出的内容到listbox            
            }
            if ((line = sr.ReadLine()) != null)
            {
                m_user = line;
                configBox.AppendText("upload user : " + line + "\n");   //增加读出的内容到listbox            
            }
            if ((line = sr.ReadLine()) != null)
            {
                m_passwd = line;
                configBox.AppendText("upload passwd : " + line + "\n");   //增加读出的内容到listbox            
            }
            if ((line = sr.ReadLine()) != null)
            {
                m_filter = line;
                configBox.AppendText("upload filter : " + line + "\n");   //增加读出的内容到listbox            
            }
            sr.Close();
        }

        public void startWatch()
        {
            m_fsWatcher.Path = m_notifyDir;
            m_fsWatcher.IncludeSubdirectories = true;
            m_fsWatcher.Filter = m_filter;
            m_fsWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
            m_fsWatcher.Changed += new FileSystemEventHandler(this.onFileChanged);
            m_fsWatcher.EnableRaisingEvents = true;
            fileBox.Clear();
        }

        public void stopWatch()
        {
            m_fsWatcher.EnableRaisingEvents = false;
            m_modifyFiles.Clear();
            fileBox.Clear();
            fileBox.AppendText("STOP STATUS.");
        }

        private void startBtn_Click(object sender, EventArgs e)
        {
            operBox.Text = "[" + DateTime.Now.ToString() + "] " + "start watch\n";
            startWatch();
        }

        private void stopBtn_Click(object sender, EventArgs e)
        {
            operBox.Text = "[" + DateTime.Now.ToString() + "] " + "stop watch\n";
            stopWatch();
        }

        private void printFileLog(string str)
        {
            fileBox.AppendText("[" + DateTime.Now.ToString()+"] "+str+"\n");
        }

        public void updateFileSet(string file)
        {
            if (!m_modifyFiles.Contains(file))
            {
                m_modifyFiles.Add(file, 1);
            }
        }

        private void onFileChanged(object sender, FileSystemEventArgs e)
        {
            updateFileSet(e.FullPath);

            printFileLog("copy from " +
                        e.FullPath);

            printFileLog("to " +
                        m_uploadDir + '/' + Path.GetDirectoryName(e.Name) +
                        "/"+Path.GetFileName(e.Name));
            string toFile = m_uploadDir + '/' + Path.GetDirectoryName(e.Name) +
                        "/"+Path.GetFileName(e.Name);
            Process pro = new Process();
            pro.StartInfo.FileName = "pscp.exe";
            pro.StartInfo.Arguments = "-sftp -pw " + m_passwd + " " + e.FullPath + " " + m_user + "@" + m_ip + ":" + toFile;
            pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            printFileLog(pro.StartInfo.FileName + " " + pro.StartInfo.Arguments);
            pro.Start();
        }

        private void refreshBtn_Click(object sender, EventArgs e)
        {
            reload();
        }

        private void diffBtn_Click(object sender, EventArgs e)
        {
            operBox.Clear();

            foreach (string file in m_modifyFiles.Keys)
            {
                operBox.AppendText(file + "\n");
            }
        }
    }
}
